<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<!-- 3、实现一个函数 find，传入一个数组和数字，返回最接近这个数字的所有元素
find([1, 2, 4, 5, 7], 3)    // [2, 4] -->

<body>
    <div>test14-3</div>
    <script>
        function find(arr, n) {
            let larger = []
            let smaller = []
            let largerMin
            let smallerMax
            arr.forEach(element => {
                element > n ? larger.push(element) : smaller.push(element)
            });
            largerMin = Math.min(...larger)
            smallerMax = Math.max(...smaller)
            return largerMin - n > n - smallerMax ? [smallerMax] : largerMin - n === n - smallerMax ? [largerMin, smallerMax] : [smallerMax]
        }
        console.log(find([1, 2, 4, 5, 7], 3))
    </script>
</body>

</html>